//package com.ruoyi.api.controller;
//
//import com.alibaba.fastjson.JSON;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.annotation.JsonAppend;
//import com.ruoyi.api.BD_Trotocol.BdDatasource;
//import com.ruoyi.api.BD_Trotocol.bean.EpiPositionBean;
//import com.ruoyi.api.entity.Message;
//import com.ruoyi.api.service.ChatUserService;
//import com.ruoyi.api.service.SerialService;
//import com.ruoyi.api.service.impl.VimMessageServiceImpl;
//import com.ruoyi.api.utils.CustomException;
//import com.ruoyi.api.utils.SerialPortUtils;
//import com.ruoyi.api.vo.ChatUser;
//import com.ruoyi.api.vo.ParamConfig;
//import com.ruoyi.api.vo.Serial;
//import com.ruoyi.common.core.domain.AjaxResult;
//import io.jsonwebtoken.lang.Strings;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.CommandLineRunner;
//import org.springframework.core.annotation.Order;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Component;
//import org.springframework.web.bind.annotation.*;
//import org.tio.utils.hutool.Snowflake;
//
//
//import javax.annotation.Resource;
//import java.util.Arrays;
//import java.util.Date;
//import java.util.List;
//import java.util.stream.IntStream;
//
//
///**
// * 串口
// */
//@Order(value = 1) // 加载顺序，值越小，优先级越高
//@Slf4j
//@Component
//@RestController
//@RequestMapping("/api/sys/serial")
//public class SerialController implements CommandLineRunner {
//
//    private BdDatasource bdDatasource;
//
//    @Autowired
//    private SerialService serialService;
//
//    @Resource
//    private RedisTemplate<String, String> redisTemplate;
//
//    @Autowired
//    private ChatUserService chatUserService;
//
//    @Autowired
//    private VimMessageServiceImpl vimMessageService;
//
//    /**
//     * 通过北斗发送消息
//     * @param args
//     */
//    @SneakyThrows
//    @Override
//    public void run (String... args) {
//        new Thread(() -> {
//            System.out.println("00000000000000000000000000");
//            // 实例化串口操作类对象
//            SerialPortUtils serialPort = new SerialPortUtils();
//            // 创建串口必要参数接收类并赋值，赋值串口号，波特率，校验位，数据位，停止位
//            ParamConfig paramConfig = new ParamConfig("COM10", 19200, 0, 8, 1);
//            String reads = null;
//            try {
//                reads = serialPort.readComm();
////                if(StringUtil.isEmpty(reads)){
////                    return;
////                }
//            } catch (CustomException e) {
//                e.printStackTrace();
//            }
//            // 初始化设置,打开串口，开始监听读取串口数据
//            try {
//                serialPort.init(paramConfig);
//            } catch (CustomException e) {
//                e.printStackTrace();
//            }
//            //便于将串口接到的信息serialEvent方法里读到的消息实时反馈到SerialPortUtils的调用处
//            String finalReads = reads;
//            serialPort.setOnSerialPortMessageListener(new SerialPortUtils.OnSerialPortMessageListener() {
//                @Override
//                public void onMsgArrive(String msg) throws Exception {
//                    Boolean result = bdDatasource.dealBDPkg(msg);
//                    if (result) {
//                        Serial serial = new Serial();
//                        serial.setDirectives(finalReads);
//                        if (msg.indexOf("BDEPI") != -1) {
//                            String[] strings = msg.split(",");
//                            String[] arr = new String[strings.length - 1];
//                            System.arraycopy(strings, 0, arr, 0, strings.length - 1);
//                            String a = Arrays.toString(arr);
//                            EpiPositionBean epiPositionBean = JSON.parseObject(a, EpiPositionBean.class);
//                            serial.setProtocol(epiPositionBean.getProtocol());
//                            serial.setTime(epiPositionBean.getTime());
//                            serial.setType(0);
//                            serial.setBDnumber(epiPositionBean.getSenderID());
//                            serial.setManner(0);
//                            serial.setContent(epiPositionBean.getLongitude() + "," + epiPositionBean.getLatitude());
////                    }else if(msg.indexOf("BDRMC") != -1){
////                        RmcLocationBean rmcLocationBean = JSON.parseObject(msg,RmcLocationBean.class);
////                        serial.setTime(rmcLocationBean.getUtmTime());
////                        serial.setType("RMC");
////                        serial.setContent(rmcLocationBean.getLongitude() + "," + rmcLocationBean.getLatitude());
////                        serial.setManner(0);
////                    }else if(msg.indexOf("BDGGA") != -1){
////                        GGABean ggaBean = JSON.parseObject(msg,GGABean.class);
////                        serial.setTime(ggaBean.getUtcTime());
////                        serial.setType("RMC");
////                        serial.setContent(ggaBean.getLongitude() + "," + ggaBean.getLatitude());
////                        serial.setManner(0);
////                    }
//                            //查询绑定的对象，查询前需要先绑定用户
//                            try {
//                                ChatUser chatUser = chatUserService.selectUser(serial.getBDnumber());
//                                //添加
//                                serialService.add(serial);
//                                //将北斗信息转化为消息对象
//                                Message message = new Message();
//                                Date date = new Date();  // 创建一个Date对象
//                                long timestamp = date.getTime();
//                                message.setId(String.valueOf(new Snowflake(1L, 1L).nextId()));
//                                message.setFromId(String.valueOf(chatUser.getNetUserId()));
//                                message.setName(chatUser.getNetUserName());
//                                message.setChatId(serial.getReceive());
//                                message.setType("0");
//                                message.setContent(serial.getContent());
//                                message.setMine(false);
//                                message.setContentType("0");
//                                message.setOffline(true);
//                                message.setTimestamp(timestamp);
//                                //使用redis放入消息缓存中
//                                //vimMessageService.save(message,true);
//                                String key = vimMessageService.getChatKey(message.getFromId(), message.getChatId(), message.getType());
//                                redisTemplate.opsForZSet().add(key, new ObjectMapper().writeValueAsString(message), message.getTimestamp());
//                            }catch (NullPointerException n){
//                                System.out.println("该北斗号未绑定用户");
//                            }
//                        }
//                    }
//                }
//            });
//        });
//    }
//
//    /**
//     * 添加对象
//     * @param
//     * @return
//     */
//    @GetMapping("add")
//    public AjaxResult addSerial() throws JsonProcessingException {
//        Serial serial = new Serial();
////        serial.setDirectives(msg);
////        String[] strings = msg.split(",");
////        String[] arr = new String[strings.length - 1];
////        System.arraycopy(strings, 0, arr, 0, strings.length - 1);
////        EpiPositionBean epiPositionBean = JSON.parseObject(msg, EpiPositionBean.class);
//        EpiPositionBean epiPositionBean = new EpiPositionBean();
//        epiPositionBean.setProtocol("$BDEPI");
//        epiPositionBean.setSenderID("1859947");
//        epiPositionBean.setReceiverID("1859947");
//        epiPositionBean.setFreqPoint(2);
//        epiPositionBean.setEmergencyId(2);
//        epiPositionBean.setPositionReportMode(1);
//        epiPositionBean.setTime("140642.71");
//        epiPositionBean.setLongitude("11821.6483333");
//        epiPositionBean.setLongitudeDirection("E");
//        epiPositionBean.setLatitude("3422.6750000");
//        epiPositionBean.setLatitudeDirection("N");
//        epiPositionBean.setHeight("28");
//        epiPositionBean.setHeightUnits("M");
//        epiPositionBean.setHeightAnomaly("-1");
//        epiPositionBean.setHeightAnomalyUnits("M");
//        epiPositionBean.setPrecision(1);
//        epiPositionBean.setMultivaluedSolution(1);
//        epiPositionBean.setRescueSubType(1);
//        epiPositionBean.setRescueCenterType(1);
//        epiPositionBean.setRescueDataLength(0);
//        epiPositionBean.setRescueData("*59");
//        String a = String.valueOf(epiPositionBean);
//        System.out.println(a);
//        serial.setProtocol(epiPositionBean.getProtocol());
//        serial.setDirectives(a);
//        serial.setTime(epiPositionBean.getTime());
//        serial.setType(0);
//        serial.setBDnumber(epiPositionBean.getSenderID());
//        serial.setManner(0);
//        serial.setContent(epiPositionBean.getLongitude() + "," + epiPositionBean.getLatitude());
//        try {
//            ChatUser chatUser = chatUserService.selectUser(serial.getBDnumber());
//            if(chatUser != null){
//                //添加
//                serialService.add(serial);
//            }
//            //将北斗信息转化为消息对象
//            Message message = new Message();
//            Date date = new Date();  // 创建一个Date对象
//            long timestamp = date.getTime();
//            message.setId(String.valueOf(new Snowflake(1L, 1L).nextId()));
//            message.setFromId(String.valueOf(chatUser.getNetUserId()));
//            message.setName(chatUser.getNetUserName());
//            message.setChatId(serial.getReceive());
//            message.setType("0");
//            message.setContent(serial.getContent());
//            message.setMine(false);
//            message.setContentType("0");
//            message.setOffline(true);
//            message.setTimestamp(timestamp);
//            //使用redis放入消息缓存中
//            //vimMessageService.save(message,true);
//            String key = vimMessageService.getChatKey(message.getFromId(), message.getChatId(), message.getType());
//            redisTemplate.opsForZSet().add(key, new ObjectMapper().writeValueAsString(message), message.getTimestamp());
//        }catch (NullPointerException n){
//            System.out.println("该北斗号未绑定用户");
//        }
//        return AjaxResult.success();
//    }
////
////    /**
////     * 删除对象(通过时间单个删除)
////     * @param serial
////     * @return
////     */
////    @PostMapping("delete")
////    public AjaxResult deleteBytime(@RequestBody Serial serial){
////        boolean a = serialService.deleteBytime(serial);
////        if(a){
////            return AjaxResult.success("删除成功");
////        }
////        return AjaxResult.error("删除失败");
////    }
////
////    /**
////     * 根据对象参数进行查询
////     * @param serial
////     * @return
////     */
////    @PostMapping("select")
////    public AjaxResult selectSerial(@RequestBody Serial serial) {
////        List<Serial> list = serialService.select(serial);
////        return AjaxResult.success(list);
////    }
//
//    /**
//     * 接收消息
//     */
//    public void acceptMessage(String bdNumber,String chatId){
//        ChatUser chatUser = chatUserService.selectUser(bdNumber);
//        List<Message> messageVoList = vimMessageService.list(String.valueOf(chatUser.getNetUserId()),chatId,"0",1L,10L);
//        Serial serial = new Serial();
//    }
//}
//
/////**
//// * 串口
//// */
//////@Order(value = 1) // 加载顺序，值越小，优先级越高
//////@Slf4j
//////@Component
////@RestController
////@RequestMapping("/api/sys/serial")
////public class SerialController {
////
////    @Autowired
////    private SerialService serialService;
////
////    @GetMapping("/add")
////    public AjaxResult add(){
////        serialService.add();
////        return AjaxResult.success("添加成功");
////    }
////
////}
